package com.gec.web.ajax;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jdt.internal.compiler.problem.ShouldNotImplement;

import com.gec.entity.Buycar;
import com.gec.entity.Member;
import com.gec.service.BuycarService;
import com.gec.service.impl.BuycarServiceImpl;
@WebServlet("/ajax/buy")
public class AjaxCarServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Member me = (Member) req.getSession().getAttribute("member");
		int gid = Integer.parseInt(req.getParameter("goodid"));
		int sid = Integer.parseInt(req.getParameter("shopid"));
		BuycarService servlet = new BuycarServiceImpl();
		/*Buycar bc = new Buycar();
		bc.setGood_id(gid);
		bc.setMem_id(me.getId());*/
		servlet.add(me.getId(),gid);
		//查询当前商品的数量
		Buycar bc2 = servlet.selectNum(me.getId(),gid);
		System.out.println(bc2);
		int num = bc2.getQty();
		Object sum = servlet.selectSum(me.getId(),sid);
		
		String json="{";
		json+="\"num\":"+num+",";
		json+="\"sum\":"+sum;
		json+="}";
		
		resp.setContentType("application/json;charset=utf-8");
		PrintWriter out = resp.getWriter();
		out.print(json);
		out.close();
	}
	
}
